package cn.steel.controller.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.steel.domain.dto.SupplierAuditDto;
import cn.steel.domain.entity.SupplierAudit;
import cn.steel.domain.entity.SupplierInfo;
import cn.steel.domain.entity.User;
import cn.steel.service.SupplierAuditService;
import cn.steel.service.SupplierInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@Controller
@RequestMapping("/manager/supplier/rating")
@Api(tags = "管理员供应商评级管理")
@PreAuthorize("hasAuthority('ADMIN')")
@RequiredArgsConstructor
public class ManagerSupplierRatingController {

    private final SupplierInfoService supplierInfoService;
    private final SupplierAuditService auditService;

    @ApiOperation("显示供应商评级列表")
    @GetMapping
    public String ratingList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String search,
            Model model) {
        log.info("显示供应商评级列表: page={}, size={}, search={}", page, size, search);
        PageHelper.startPage(page, size);
        Page<SupplierInfo> pageResult = (Page<SupplierInfo>) supplierInfoService.findAllPaginated(search);
        model.addAttribute("suppliers", pageResult.getResult());
        model.addAttribute("totalPages", pageResult.getPages());
        model.addAttribute("totalItems", pageResult.getTotal());
        model.addAttribute("currentPage", pageResult.getPageNum());
        model.addAttribute("pageSize", size);
        model.addAttribute("search", search);

        // 添加默认的 auditDto 对象
        SupplierAuditDto auditDto = new SupplierAuditDto();
        model.addAttribute("auditDto", auditDto);

        return "manager/supplier_rating_list";
    }

    @ApiOperation("提交供应商资质审核")
    @PostMapping("/audit")
    public String submitAudit(
            @SessionAttribute(value = "user", required = false) User user,
            @Valid @ModelAttribute SupplierAuditDto auditDto,
            BindingResult result,
            Model model) {
        log.info("提交供应商资质审核: auditDto={}", auditDto);
        if (result.hasErrors()) {
            // 错误时重新加载列表页面
            PageHelper.startPage(1, 10);
            Page<SupplierInfo> pageResult = (Page<SupplierInfo>) supplierInfoService.findAllPaginated(null);
            model.addAttribute("suppliers", pageResult.getResult());
            model.addAttribute("totalPages", pageResult.getPages());
            model.addAttribute("totalItems", pageResult.getTotal());
            model.addAttribute("currentPage", 1);
            model.addAttribute("pageSize", 10);
            model.addAttribute("auditDto", auditDto);
            model.addAttribute("auditError", true);
            model.addAttribute("supplierId", auditDto.getSupplierId());
            return "manager/supplier_rating_list";
        }

        SupplierAudit audit = BeanUtil.toBean(auditDto, SupplierAudit.class);
        SupplierInfo supplierInfo = supplierInfoService.getSupplierInfoByUserId(audit.getSupplierId());
        audit.setLicenseUrl(supplierInfo.getLicenseUrl());
        auditService.submitAudit(user.getId(), audit);
        return "redirect:/manager/supplier/rating?success=true";
    }
}